package com.epro.dx.widget;

import com.epro.dx.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;

/**
 * 带有清楚按钮的编辑框
 * @author huangfei
 *
 */
public class EditTextWithClear extends EditText implements OnTouchListener,
		OnFocusChangeListener {
	
	private Drawable clearDrawable;

	public EditTextWithClear(Context context) {
		super(context);
		init();
	}

	public EditTextWithClear(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public EditTextWithClear(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		Log.i("---", "onTouch---");
		if (getCompoundDrawables()[2] != null) {
			//Touch是否为清除按钮所在区域
			boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - clearDrawable
					.getIntrinsicWidth());
			if (tappedX) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					setText("");
				}
				return true;
			}
		}
		return false;
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			setClearIconVisible(!TextUtils.isEmpty(getText()));
		} else {
			setClearIconVisible(false);
		}
	}

	private void init() {
		clearDrawable = getCompoundDrawables()[2];
		if (clearDrawable == null) {
			clearDrawable = getResources().getDrawable(getDefaultClearIconId());
		}
		clearDrawable.setBounds(0, 0, clearDrawable.getIntrinsicWidth(), clearDrawable.getIntrinsicHeight());
		setClearIconVisible(false);
//		addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before,
//					int count) {
//				// TODO Auto-generated method stub
//				setClearIconVisible(!TextUtils.isEmpty(getText()));
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count,
//					int after) {
//
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//			}
//		});
		// this.setPaddingRelative(getPaddingLeft(), getPaddingTop(),
		// getPaddingRight(), getPaddingBottom());
		//根据是否显示ClearIcon来注册焦点事件和Touch事件
			setOnTouchListener(this);
			setOnFocusChangeListener(this);
	}

	private int getDefaultClearIconId() {
		int id = R.drawable.edittext_clear;
		return id;
	}

	public void setClearIconVisible(boolean visible) {
		Drawable x = visible ? clearDrawable : null;
		setCompoundDrawables(getCompoundDrawables()[0],
				getCompoundDrawables()[1], x, getCompoundDrawables()[3]);
	}
	
	/**
	 * EditText是否需要清除全部的按钮
	 * @param isShow
	 */
	public void isNeedShowClearBtn(boolean isShow){
		if(!isShow){
			setOnTouchListener(null);
			setOnFocusChangeListener(null);
		}
	}

}